{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Title: #Rearrange Array Elements by Sign"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Difficulty: #Medium"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Category Title: #Algorithms"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Tag Slug: #array #two-pointers #simulation"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Name Translated: #数组 #双指针 #模拟"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solution Name: rearrangeArray"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Title: #按符号重排数组"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Content:\n",
    "<p>给你一个下标从 <strong>0</strong> 开始的整数数组 <code>nums</code> ，数组长度为 <strong>偶数</strong> ，由数目相等的正整数和负整数组成。</p>\n",
    "\n",
    "<p>你需要 <strong>重排</strong> <code>nums</code> 中的元素，使修改后的数组满足下述条件：</p>\n",
    "\n",
    "<ol>\n",
    "\t<li>任意&nbsp;<strong>连续</strong> 的两个整数 <strong>符号相反</strong></li>\n",
    "\t<li>对于符号相同的所有整数，<strong>保留</strong> 它们在 <code>nums</code> 中的 <strong>顺序</strong> 。</li>\n",
    "\t<li>重排后数组以正整数开头。</li>\n",
    "</ol>\n",
    "\n",
    "<p>重排元素满足上述条件后，返回修改后的数组。</p>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>示例 1：</strong></p>\n",
    "\n",
    "<pre>\n",
    "<strong>输入：</strong>nums = [3,1,-2,-5,2,-4]\n",
    "<strong>输出：</strong>[3,-2,1,-5,2,-4]\n",
    "<strong>解释：</strong>\n",
    "nums 中的正整数是 [3,1,2] ，负整数是 [-2,-5,-4] 。\n",
    "重排的唯一可行方案是 [3,-2,1,-5,2,-4]，能满足所有条件。\n",
    "像 [1,-2,2,-5,3,-4]、[3,1,2,-2,-5,-4]、[-2,3,-5,1,-4,2] 这样的其他方案是不正确的，因为不满足一个或者多个条件。 \n",
    "</pre>\n",
    "\n",
    "<p><strong>示例 2：</strong></p>\n",
    "\n",
    "<pre>\n",
    "<strong>输入：</strong>nums = [-1,1]\n",
    "<strong>输出：</strong>[1,-1]\n",
    "<strong>解释：</strong>\n",
    "1 是 nums 中唯一一个正整数，-1 是 nums 中唯一一个负整数。\n",
    "所以 nums 重排为 [1,-1] 。\n",
    "</pre>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>提示：</strong></p>\n",
    "\n",
    "<ul>\n",
    "\t<li><code>2 &lt;= nums.length &lt;= 2 * 10<sup>5</sup></code></li>\n",
    "\t<li><code>nums.length</code> 是 <strong>偶数</strong></li>\n",
    "\t<li><code>1 &lt;= |nums[i]| &lt;= 10<sup>5</sup></code></li>\n",
    "\t<li><code>nums</code> 由 <strong>相等</strong> 数量的正整数和负整数组成</li>\n",
    "</ul>\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Description: [rearrange-array-elements-by-sign](https://leetcode.cn/problems/rearrange-array-elements-by-sign/description/)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solutions: [rearrange-array-elements-by-sign](https://leetcode.cn/problems/rearrange-array-elements-by-sign/solutions/)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "test_cases = ['[3,1,-2,-5,2,-4]', '[-1,1]']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def rearrangeArray(self, nums: List[int]) -> List[int]:\n",
    "        n = len(nums)\n",
    "        ans = [0] * n \n",
    "        ans[::2] = [x for x in nums if x > 0]\n",
    "        ans[1::2] = [x for x in nums if x < 0]\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def rearrangeArray(self, nums: List[int]) -> List[int]:\n",
    "        n = len(nums)\n",
    "        ans = [0] * n \n",
    "        ans[::2] = [x for x in nums if x > 0]\n",
    "        ans[1::2] = [x for x in nums if x < 0]\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def rearrangeArray(self, nums: List[int]) -> List[int]:\n",
    "        i = 0\n",
    "        j = 1\n",
    "        r = len(nums) * [0]\n",
    "        for num in nums:\n",
    "            if num > 0:\n",
    "                r[i] = num\n",
    "                i += 2\n",
    "            else:\n",
    "                r[j] = num\n",
    "                j += 2\n",
    "        return r"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def rearrangeArray(self, nums: List[int]) -> List[int]:\n",
    "        neg = [num for num in nums if num <0]\n",
    "        pos = [num for num in nums if num >0]\n",
    "        nums[::2] = pos \n",
    "        nums[1::2] = neg \n",
    "        return nums\n",
    "a = Solution()\n",
    "a.rearrangeArray(nums=[3,1,-2,-5,2,-4])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def rearrangeArray(self, nums: List[int]) -> List[int]:\n",
    "        pos = [num for num in nums if num > 0]\n",
    "        neg = [num for num in nums if num < 0]\n",
    "        nums[::2] = pos\n",
    "        nums[1::2] = neg\n",
    "\n",
    "        return nums\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def rearrangeArray(self, nums: List[int]) -> List[int]:\n",
    "        neg = [num for num in nums if num <0]\n",
    "        pos = [num for num in nums if num >0]\n",
    "        nums[::2] = pos \n",
    "        nums[1::2] = neg \n",
    "        return nums"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def rearrangeArray(self, nums: List[int]) -> List[int]:\n",
    "        i,j=0,1\n",
    "        res=[0]*len(nums)\n",
    "        for num in nums:\n",
    "            if num>0:\n",
    "                res[i]=num\n",
    "                i+=2\n",
    "            else:\n",
    "                res[j]=num\n",
    "                j+=2\n",
    "        return res\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def rearrangeArray(self, nums: List[int]) -> List[int]:\n",
    "        neg = [num for num in nums if num <0]\n",
    "        pos = [num for num in nums if num >0]\n",
    "        nums[::2] = pos \n",
    "        nums[1::2] = neg \n",
    "        return nums"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def rearrangeArray(self, nums: List[int]) -> List[int]:\n",
    "        res = [0] * len(nums)\n",
    "        even = 0\n",
    "        odd = 1\n",
    "        for num in nums:\n",
    "            if num > 0:\n",
    "                res[even] = num\n",
    "                even += 2\n",
    "            else:\n",
    "                res[odd] = num\n",
    "                odd += 2\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def rearrangeArray(self, nums: List[int]) -> List[int]:\n",
    "        f_l = [i for i in nums if i < 0]\n",
    "        z_l = [i for i in nums if i > 0]\n",
    "\n",
    "        for i in range(len(nums)):\n",
    "            if i % 2 == 0:\n",
    "                nums[i] = z_l[i // 2]\n",
    "\n",
    "            else:\n",
    "                nums[i] = f_l[i // 2]\n",
    "        return nums"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def rearrangeArray(self, nums: List[int]) -> List[int]:\n",
    "        p = [[],[]]\n",
    "        cur = 0\n",
    "        for i in range(len(nums)):\n",
    "            if nums[i] > 0:\n",
    "                p[0].append(nums[i])\n",
    "            else:\n",
    "                p[1].append(nums[i])\n",
    "            if p[0] and p[1]:\n",
    "                nums[cur] = p[0].pop(0)\n",
    "                nums[cur+1] = p[1].pop(0)\n",
    "                cur += 2\n",
    "        return nums"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def rearrangeArray(self, nums: List[int]) -> List[int]:\n",
    "        lst1, lst2 = [], []\n",
    "        for n in nums:\n",
    "            if n > 0: lst1.append(n)\n",
    "            else    : lst2.append(n)\n",
    "        nums[::2], nums[1::2] = lst1, lst2\n",
    "        return nums"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def rearrangeArray(self, nums: List[int]) -> List[int]:\n",
    "        l1=[]\n",
    "        l2=[]\n",
    "        for i in nums:\n",
    "            if i>0:\n",
    "                l1.append(i)\n",
    "            else:\n",
    "                l2.append(i)\n",
    "        nums[::2]=l1\n",
    "        nums[1::2]=l2\n",
    "        return nums"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def rearrangeArray(self, nums: List[int]) -> List[int]:\n",
    "\n",
    "\n",
    "        pos = []\n",
    "        neg = []\n",
    "        for x in nums:\n",
    "            if x>0:\n",
    "                pos.append(x)\n",
    "            elif x<0:\n",
    "                neg.append(x)\n",
    "        \n",
    "        for i in range(len(pos)):\n",
    "            nums[2*i] = pos[i]\n",
    "            nums[2*i+1] = neg[i]\n",
    "        return nums\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def rearrangeArray(self, nums: List[int]) -> List[int]:\n",
    "        # flag = 1\n",
    "        # for i in range(len(nums)):\n",
    "        #     for j in range(i,len(nums)):\n",
    "        #         if flag * nums[j] > 0:\n",
    "        #             tmp = nums[j]\n",
    "        #             nums.\n",
    "        #             nums = nums[:i] + [] + nums[i:j] + (nums[j+1:] if j !=n else [])\n",
    "        #             flag = -flag\n",
    "        #             break\n",
    "        # return nums\n",
    "        lst1, lst2 = [], []\n",
    "        for n in nums:\n",
    "            if n > 0: lst1.append(n)\n",
    "            else    : lst2.append(n)\n",
    "        nums[::2], nums[1::2] = lst1, lst2\n",
    "        return nums"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def rearrangeArray(self, nums: List[int]) -> List[int]:\n",
    "        pos, neg = [], []\n",
    "        for n in nums:\n",
    "            if n > 0:\n",
    "                pos.append(n)\n",
    "            else:\n",
    "                neg.append(n)\n",
    "        nums[::2], nums[1::2] = pos, neg\n",
    "        return nums"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def rearrangeArray(self, nums: List[int]) -> List[int]:\n",
    "        lst1, lst2 = [], []\n",
    "        for n in nums:\n",
    "            if n > 0: \n",
    "                lst1.append(n)\n",
    "            else: \n",
    "                lst2.append(n)\n",
    "        nums[::2], nums[1::2] = lst1, lst2\n",
    "        return nums\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "# leetcode submit region begin(Prohibit modification and deletion)\n",
    "from typing import List\n",
    "from collections import deque\n",
    "\n",
    "class Solution:\n",
    "\n",
    "    def rearrangeArray(self, nums: List[int]) -> List[int]:\n",
    "        neg_nums = deque()\n",
    "        pos_nums = deque()\n",
    "        res = []\n",
    "        for item in nums:\n",
    "            if item >= 0:\n",
    "                pos_nums.append(item)\n",
    "            else:\n",
    "                neg_nums.append(item)\n",
    "\n",
    "        for i in range(len(nums)):\n",
    "            if i == 0 or i % 2 == 0:\n",
    "                res.append(pos_nums.popleft())\n",
    "            else:\n",
    "                res.append(neg_nums.popleft())\n",
    "\n",
    "        return res\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "# leetcode submit region end(Prohibit modification and deletion)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def rearrangeArray(self, nums: List[int]) -> List[int]:\n",
    "        ret = []\n",
    "        # for num in nums:\n",
    "        pos,neg,n = 0,0,len(nums)\n",
    "        counteract = 1\n",
    "        while pos<n or neg<n:\n",
    "            if counteract:\n",
    "                if pos==n:\n",
    "                    counteract = 0\n",
    "                    continue\n",
    "                if nums[pos] > 0:\n",
    "                    ret.append(nums[pos])\n",
    "                    counteract = 0\n",
    "                pos += 1\n",
    "            else:\n",
    "                if neg==n:\n",
    "                    counteract = 1\n",
    "                    continue\n",
    "                if nums[neg] < 0:\n",
    "                    ret.append(nums[neg])\n",
    "                    counteract = 1\n",
    "                neg += 1\n",
    "        return ret\n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "# 双指针\n",
    "class Solution:\n",
    "    def rearrangeArray(self, nums: List[int]) -> List[int]:\n",
    "        res,p,n = [],0,0\n",
    "        for i in range(len(nums)//2):\n",
    "            while nums[p] < 0:\n",
    "                p += 1\n",
    "            res.append(nums[p])\n",
    "            p += 1\n",
    "            while nums[n] > 0:\n",
    "                n += 1\n",
    "            res.append(nums[n])\n",
    "            n += 1\n",
    "        return res\n",
    "        \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def rearrangeArray(self, nums: List[int]) -> List[int]:\n",
    "        pos_nums = []\n",
    "        neg_nums = []\n",
    "        for num in nums:\n",
    "            if num > 0:\n",
    "                pos_nums.append(num)\n",
    "            else:\n",
    "                neg_nums.append(num)\n",
    "\n",
    "        nums[::2] = pos_nums\n",
    "        nums[1::2] = neg_nums\n",
    "        return nums"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def rearrangeArray(self, nums: List[int]) -> List[int]:\n",
    "        ret = []\n",
    "        # for num in nums:\n",
    "        pos,neg,n = 0,0,len(nums)\n",
    "        counteract = 1\n",
    "        while pos<n or neg<n:\n",
    "            if counteract:\n",
    "                if pos==n:\n",
    "                    counteract = 0\n",
    "                    continue\n",
    "                if nums[pos] > 0:\n",
    "                    ret.append(nums[pos])\n",
    "                    counteract = 0\n",
    "                pos += 1\n",
    "            else:\n",
    "                if neg==n:\n",
    "                    counteract = 1\n",
    "                    continue\n",
    "                if nums[neg] < 0:\n",
    "                    ret.append(nums[neg])\n",
    "                    counteract = 1\n",
    "                neg += 1\n",
    "        return ret\n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def rearrangeArray(self, nums: List[int]) -> List[int]:\n",
    "        n = len(nums)\n",
    "        pos = neg = 0\n",
    "        ans = list()\n",
    "\n",
    "        for i in range(n // 2):\n",
    "            while nums[pos] < 0:\n",
    "                pos += 1\n",
    "            ans.append(nums[pos])\n",
    "            pos += 1\n",
    "            while nums[neg] > 0:\n",
    "                neg += 1\n",
    "            ans.append(nums[neg])\n",
    "            neg += 1\n",
    "        \n",
    "        return ans\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def rearrangeArray(self, nums: List[int]) -> List[int]:\n",
    "        if not nums: return nums\n",
    "\n",
    "        length = len(nums)\n",
    "        left = right = 0\n",
    "        res = list()\n",
    "        for idx in range(length // 2):\n",
    "            while nums[left] < 0:\n",
    "                left += 1\n",
    "            res.append(nums[left])\n",
    "            left += 1\n",
    "            while nums[right] > 0:\n",
    "                right += 1\n",
    "            res.append(nums[right])\n",
    "            right += 1\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def rearrangeArray(self, nums: List[int]) -> List[int]:\n",
    "        lst1, lst2 = [], []\n",
    "        for n in nums:\n",
    "            if n > 0: lst1.append(n)\n",
    "            else    : lst2.append(n)\n",
    "        nums[::2], nums[1::2] = lst1, lst2\n",
    "        return nums"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\r\n",
    "    def rearrangeArray(self, nums: List[int]) -> List[int]:\r\n",
    "        ans = [0] * len(nums)\r\n",
    "        pos = 0\r\n",
    "        neg = 1\r\n",
    "        for i in range(len(nums)):\r\n",
    "            if nums[i] > 0:\r\n",
    "                ans[pos] = nums[i]\r\n",
    "                pos += 2\r\n",
    "            else:\r\n",
    "                ans[neg] = nums[i]\r\n",
    "                neg += 2\r\n",
    "        return(ans)\r\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def rearrangeArray(self, nums: List[int]) -> List[int]:\n",
    "        n=len(nums)\n",
    "        ans=[0 for i in range(n)]\n",
    "        p=0\n",
    "        q=1\n",
    "        for i in nums:\n",
    "            if i>0:\n",
    "                ans[p]=i\n",
    "                p+=2\n",
    "            elif i<0:\n",
    "                ans[q]=i\n",
    "                q+=2\n",
    "        return ans\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def rearrangeArray(self, nums: List[int]) -> List[int]:\n",
    "        ans = list()\n",
    "        neg = 0\n",
    "        pos = 0\n",
    "        n = len(nums)\n",
    "        for i in range(n // 2):\n",
    "            while nums[pos] < 0:\n",
    "                pos += 1\n",
    "            ans.append(nums[pos])\n",
    "            pos += 1\n",
    "            while nums[neg] > 0:\n",
    "                neg += 1\n",
    "            ans.append(nums[neg])\n",
    "            neg += 1\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def rearrangeArray(self, nums: List[int]) -> List[int]:\n",
    "        n = len(nums)\n",
    "        pos, ne = 0 , 1\n",
    "        r = [0] * n \n",
    "        for i in range(n):\n",
    "            num = nums[i]\n",
    "            if num > 0:\n",
    "                r[pos] = num \n",
    "                pos += 2\n",
    "            else:\n",
    "                r[ne] = num \n",
    "                ne += 2\n",
    "        return r"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def rearrangeArray(self, nums: List[int]) -> List[int]:\n",
    "        n = len(nums)\n",
    "        pos, neg = 0, 0\n",
    "        ans = []\n",
    "        for i in range(n//2):\n",
    "            while nums[pos] < 0:\n",
    "                pos += 1\n",
    "            ans.append(nums[pos])\n",
    "            pos += 1\n",
    "            while nums[neg] > 0:\n",
    "                neg += 1\n",
    "            ans.append(nums[neg])\n",
    "            neg += 1\n",
    "        return ans\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def rearrangeArray(self, nums: List[int]) -> List[int]:\n",
    "        n=len(nums)\n",
    "        pos=neg=0\n",
    "        ans=list()\n",
    "\n",
    "        for i in range(n//2):\n",
    "            while nums[pos]<0:\n",
    "                pos+=1\n",
    "            ans.append(nums[pos])\n",
    "            pos+=1\n",
    "            while nums[neg]>0:\n",
    "                neg+=1\n",
    "            ans.append(nums[neg])\n",
    "            neg+=1\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def rearrangeArray(self, nums: List[int]) -> List[int]:\n",
    "        pos = neg = 0\n",
    "        ans = []\n",
    "        n = len(nums)\n",
    "        for _ in range(n >> 1):\n",
    "            while nums[pos] < 0:\n",
    "                pos += 1\n",
    "            ans.append(nums[pos])\n",
    "            pos += 1\n",
    "\n",
    "            while nums[neg] > 0:\n",
    "                neg += 1\n",
    "            ans.append(nums[neg])\n",
    "            neg += 1\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def rearrangeArray(self, nums: List[int]) -> List[int]:\n",
    "        if not nums: return nums\n",
    "        \n",
    "        res = list()\n",
    "        left = right = 0\n",
    "        for idx in range(len(nums) // 2):\n",
    "            while (nums[left] < 0): left += 1\n",
    "            res.append(nums[left])\n",
    "            left += 1\n",
    "            while (nums[right] > 0): right += 1\n",
    "            res.append(nums[right])\n",
    "            right += 1\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def rearrangeArray(self, nums: List[int]) -> List[int]:\n",
    "        n=len(nums)\n",
    "        ans=[]\n",
    "        positive,negative=0,0\n",
    "        for _ in range(0,n//2):\n",
    "            # 找到之后的第一个正数和负数\n",
    "            while nums[positive]<0:\n",
    "                positive+=1\n",
    "            while nums[negative]>0:\n",
    "                negative+=1\n",
    "            ans.extend([nums[positive],nums[negative]])\n",
    "            positive+=1\n",
    "            negative+=1\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def rearrangeArray(self, nums: List[int]) -> List[int]:\n",
    "        ans=[0 for i in range(len(nums))]\n",
    "        pos1,pos2=0,1\n",
    "        for num in nums :\n",
    "            if num<0 :\n",
    "                ans[pos2]=num\n",
    "                pos2+=2\n",
    "            else:\n",
    "                ans[pos1]=num\n",
    "                pos1+=2\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def rearrangeArray(self, nums: List[int]) -> List[int]:\n",
    "        n = len(nums)\n",
    "        pos = neg = 0\n",
    "        ans = list()\n",
    "        \n",
    "        for i in range(n // 2):\n",
    "            while nums[pos] < 0:\n",
    "                pos += 1\n",
    "            ans.append(nums[pos])\n",
    "            pos += 1\n",
    "            while nums[neg] > 0:\n",
    "                neg += 1\n",
    "            ans.append(nums[neg])\n",
    "            neg += 1\n",
    "\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def rearrangeArray(self, nums: List[int]) -> List[int]:\n",
    "        p = []\n",
    "        n = []\n",
    "        for x in nums:\n",
    "            if x < 0:\n",
    "                n.append(x)\n",
    "            else:\n",
    "                p.append(x)\n",
    "        \n",
    "        ans = []\n",
    "        for v1, v2 in zip(p, n):\n",
    "            ans.append(v1)\n",
    "            ans.append(v2)\n",
    "        \n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def rearrangeArray(self, nums: List[int]) -> List[int]:\n",
    "        n = len(nums)\n",
    "        pos, neg = 0, 0\n",
    "        ans = []\n",
    "        for i in range(n//2):\n",
    "            while nums[pos] < 0:\n",
    "                pos += 1\n",
    "            ans.append(nums[pos])\n",
    "            pos += 1\n",
    "            while nums[neg] > 0:\n",
    "                neg += 1\n",
    "            ans.append(nums[neg])\n",
    "            neg += 1\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def rearrangeArray(self, nums: List[int]) -> List[int]:\n",
    "        p1 = []\n",
    "        p2 = []\n",
    "        for i in nums:\n",
    "            if i>0:\n",
    "                p1.append(i)\n",
    "            else:\n",
    "                p2.append(i)\n",
    "        ans = []\n",
    "        for i in range(len(p1)):\n",
    "            ans.append(p1[i])\n",
    "            ans.append(p2[i])\n",
    "        return ans "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def rearrangeArray(self, nums: List[int]) -> List[int]:\n",
    "        pos, neg = [], []\n",
    "        for num in nums:\n",
    "            if num > 0:\n",
    "                pos.append(num)\n",
    "            else:\n",
    "                neg.append(num)\n",
    "        \n",
    "        ans = []\n",
    "        n = len(pos)\n",
    "        for i in range(n):\n",
    "            ans.append(pos[i])\n",
    "            ans.append(neg[i])\n",
    "        \n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def rearrangeArray(self, nums: List[int]) -> List[int]:\n",
    "        n=len(nums)\n",
    "        ret=[0]*n\n",
    "        pos,neg=0,0\n",
    "        for i in range(0,n,2):\n",
    "            while pos<n and nums[pos]<0:\n",
    "                pos+=1\n",
    "            ret[i]=nums[pos]\n",
    "            pos+=1\n",
    "            while neg<n and nums[neg]>0:\n",
    "                neg+=1\n",
    "            ret[i+1]=nums[neg]\n",
    "            neg+=1\n",
    "        return ret"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def rearrangeArray(self, nums: List[int]) -> List[int]:\n",
    "        negative = []\n",
    "        positive = []\n",
    "        ans = []\n",
    "\n",
    "        for num in nums:\n",
    "            if num > 0:\n",
    "                positive.append(num)\n",
    "            else:\n",
    "                negative.append(num)\n",
    "        \n",
    "        for idx, num in enumerate(positive):\n",
    "            ans.append(num)\n",
    "            ans.append(negative[idx])\n",
    "        \n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def rearrangeArray(self, nums: List[int]) -> List[int]:\n",
    "        fushu = [num for num in nums if num < 0]\n",
    "        zhengshu = [num for num in nums if num > 0]\n",
    "        z = 0\n",
    "        f = 0\n",
    "        ans = []\n",
    "        for i in range(len(nums)): \n",
    "            if i % 2 == 0:\n",
    "                ans.append(zhengshu[z])\n",
    "                z += 1\n",
    "            else: \n",
    "                ans.append(fushu[f])\n",
    "                f += 1\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def rearrangeArray(self, nums: List[int]) -> List[int]:\n",
    "        l1 = [i  for i in nums if i>0]\n",
    "        l2 = [i for i in nums if i<0 ]\n",
    "        l = []\n",
    "        for i in range(len(nums)//2):\n",
    "            l.append(l1[i])\n",
    "            l.append(l2[i])\n",
    "\n",
    "        return l"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def rearrangeArray(self, nums: List[int]) -> List[int]:\n",
    "        i = 1\n",
    "        j = 0\n",
    "        data_list = []\n",
    "        data_list2 = []\n",
    "        for data in nums:\n",
    "        \n",
    "            if data < 0:\n",
    "                data_list.append(data)\n",
    "            else:\n",
    "                data_list2.append(data)\n",
    "\n",
    "        \n",
    "        while j < len(data_list):\n",
    "            data_list2.insert(i,data_list[j])\n",
    "            i += 2\n",
    "            j += 1\n",
    "        return data_list2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def rearrangeArray(self, nums: List[int]) -> List[int]:\n",
    "        n = len(nums)\n",
    "        ans = [None] * n\n",
    "        biger_than_zero_ptr, smaller_than_zero_ptr, ans_ptr = 0, 0, 0\n",
    "        while biger_than_zero_ptr < n or smaller_than_zero_ptr < n:\n",
    "            while biger_than_zero_ptr < n and nums[biger_than_zero_ptr] < 0:\n",
    "                biger_than_zero_ptr += 1\n",
    "            if biger_than_zero_ptr < n:\n",
    "                ans[ans_ptr] = nums[biger_than_zero_ptr]\n",
    "                ans_ptr += 1\n",
    "                biger_than_zero_ptr += 1\n",
    "            while smaller_than_zero_ptr < n and nums[smaller_than_zero_ptr] > 0:\n",
    "                smaller_than_zero_ptr += 1\n",
    "            if smaller_than_zero_ptr < n:\n",
    "                ans[ans_ptr] = nums[smaller_than_zero_ptr]\n",
    "                ans_ptr += 1\n",
    "                smaller_than_zero_ptr += 1\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def rearrangeArray(self, nums: List[int]) -> List[int]:\n",
    "        \n",
    "        pos = []\n",
    "        neg = []\n",
    "        \n",
    "        for i in range(len(nums)):\n",
    "            \n",
    "            if nums[i] > 0:\n",
    "                pos.append(nums[i])\n",
    "            else:\n",
    "                neg.append(nums[i])\n",
    "        \n",
    "        res = []\n",
    "        \n",
    "        for i in range(0, len(pos)):\n",
    "            res.append(pos[i])\n",
    "            res.append(neg[i])\n",
    "        \n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def rearrangeArray(self, nums: List[int]) -> List[int]:\n",
    "        lst1 = []\n",
    "        lst2 = []\n",
    "        for v in nums:\n",
    "            if v >= 0:\n",
    "                lst1.append(v)\n",
    "            else:\n",
    "                lst2.append(v)\n",
    "        res = []\n",
    "        for i in range(len(lst1)):\n",
    "            res.append(lst1[i])\n",
    "            res.append(lst2[i])\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def rearrangeArray(self, nums: List[int]) -> List[int]:\n",
    "        pos = 0\n",
    "        neg = 0\n",
    "\n",
    "        res = []\n",
    "        LEN = len(nums)\n",
    "        while pos<LEN and neg<LEN:\n",
    "            while pos < LEN and nums[pos]<0:\n",
    "                pos+=1\n",
    "            while neg < LEN and nums[neg]>0:\n",
    "                neg+=1\n",
    "            res.append(nums[pos])\n",
    "            pos+=1\n",
    "            res.append(nums[neg])\n",
    "            neg+=1\n",
    "        \n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def rearrangeArray(self, nums: List[int]) -> List[int]:\n",
    "        n = len(nums)\n",
    "        pos, neg = 0, 1\n",
    "        ans = [0] * n\n",
    "        for num in nums:\n",
    "            if num > 0:\n",
    "                ans[pos] = num \n",
    "                pos += 2 \n",
    "            else:\n",
    "                ans[neg] = num \n",
    "                neg += 2\n",
    "        return ans\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def rearrangeArray(self, nums: List[int]) -> List[int]:\n",
    "        l,r = [],[]\n",
    "        for i in nums:\n",
    "            if i > 0:\n",
    "                l.append(i)\n",
    "            else:\n",
    "                r.append(i)\n",
    "        ans = []\n",
    "        for i in range(len(l)):\n",
    "            ans.append(l[i])\n",
    "            ans.append(r[i])\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def rearrangeArray(self, nums: List[int]) -> List[int]:\n",
    "        n = len(nums)\n",
    "        pos = []\n",
    "        neg = []\n",
    "        ans = []\n",
    "        for i in range(n):\n",
    "            pos.append(nums[i]) if nums[i] > 0 else neg.append(nums[i])\n",
    "        for j in range(n//2):\n",
    "            ans.append(pos[j])\n",
    "            ans.append(neg[j])\n",
    "        return ans\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def rearrangeArray(self, nums: List[int]) -> List[int]:\n",
    "        n = len(nums)\n",
    "        pos = []\n",
    "        neg = []\n",
    "        for i in range(n):\n",
    "            if nums[i] > 0:\n",
    "                pos.append(nums[i])\n",
    "            else:\n",
    "                neg.append(nums[i])\n",
    "        ans = []\n",
    "        for i in range(n//2):\n",
    "            ans.append(pos[i])\n",
    "            ans.append(neg[i])\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def rearrangeArray(self, nums: List[int]) -> List[int]:\n",
    "        arr1 = []\n",
    "        arr2 = []\n",
    "        for i in range(len(nums)):\n",
    "            if nums[i] > 0:\n",
    "                arr1.append(nums[i])\n",
    "            else:\n",
    "                arr2.append(nums[i])\n",
    "\n",
    "        return [item for pair in zip(arr1, arr2) for item in pair]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def rearrangeArray(self, nums: List[int]) -> List[int]:\n",
    "        itPos = filter(lambda n: n > 0, iter(nums))\n",
    "        itNeg = filter(lambda n: n < 0, iter(nums))\n",
    "        return list(chain.from_iterable(zip(itPos, itNeg)))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def rearrangeArray(self, nums: List[int]) -> List[int]:\n",
    "        n = len(nums)\n",
    "        pn = []\n",
    "        nn = []\n",
    "        ans = []\n",
    "        for num in nums:\n",
    "            if num < 0:\n",
    "                nn.append(num)\n",
    "            else:\n",
    "                pn.append(num)\n",
    "        i = 0\n",
    "        while i < n/2:\n",
    "            ans.append(pn[i])\n",
    "            ans.append(nn[i])\n",
    "            i += 1\n",
    "        return ans\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def rearrangeArray(self, nums: List[int]) -> List[int]:\n",
    "        zheng = []\n",
    "        fu = []\n",
    "        for i in nums:\n",
    "            if i > 0:\n",
    "                zheng.append(i)\n",
    "            if i < 0:\n",
    "                fu.append(i)\n",
    "        res = []\n",
    "        for i in range(len(nums) // 2):\n",
    "            res.append(zheng[i])\n",
    "            res.append(fu[i])\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def rearrangeArray(self, nums: List[int]) -> List[int]:\n",
    "      return [*chain(*zip((x for x in nums if x > 0), (x for x in nums if x < 0)))]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def rearrangeArray(self, nums: List[int]) -> List[int]:\n",
    "        return [*chain(*zip((x for x in nums if x > 0), (x for x in nums if x < 0)))]\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def rearrangeArray(self, nums: List[int]) -> List[int]:\n",
    "        return [*chain(*zip((x for x in nums if x > 0), (x for x in nums if x < 0)))]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "\n",
    "class Solution:\n",
    "    def rearrangeArray(self, nums: List[int]) -> List[int]:\n",
    "        return [*chain(*zip((x for x in nums if x > 0), (x for x in nums if x < 0)))]\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def rearrangeArray(self, nums: List[int]) -> List[int]:\n",
    "        lst_a, lst_b = [], []\n",
    "        for n in nums:\n",
    "            if n > 0:\n",
    "                lst_a.append(n)\n",
    "            else:\n",
    "                lst_b.append(n)\n",
    "        ans = []\n",
    "        for a, b in zip(lst_a, lst_b):\n",
    "            ans += [a, b]\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def rearrangeArray(self, nums: List[int]) -> List[int]:\n",
    "        zheng_nums, fu_nums = [], []\n",
    "        for i in range(len(nums)):\n",
    "            if nums[i] > 0:\n",
    "                zheng_nums.append(nums[i])\n",
    "            elif nums[i] < 0:\n",
    "                fu_nums.append(nums[i])\n",
    "        res = []\n",
    "        for i in range(len(zheng_nums)):\n",
    "            res.append(zheng_nums[i])\n",
    "            res.append(fu_nums[i])\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def rearrangeArray(self, nums: List[int]) -> List[int]:\n",
    "        odd = []\n",
    "        even = []\n",
    "        for i, v in enumerate(nums):\n",
    "            if v > 0:\n",
    "                odd.append(i)\n",
    "            else:\n",
    "                even.append(i)\n",
    "        \n",
    "        res = []\n",
    "        for i, j in zip(odd, even):\n",
    "            res.__iadd__([nums[i], nums[j]])\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def rearrangeArray(self, nums: List[int]) -> List[int]:\n",
    "        l = len(nums)\n",
    "        zheng = []\n",
    "        fu = []\n",
    "        res = []\n",
    "\n",
    "        for num in nums:\n",
    "            if num > 0:\n",
    "                zheng.append(num)\n",
    "            else:\n",
    "                fu.append(num)\n",
    "        #print(zheng,fu)\n",
    "        for i in range(l):\n",
    "            if i%2 == 0:\n",
    "                res.append(zheng[i//2])\n",
    "            else:\n",
    "                res.append(fu[int((i-1)/2)])\n",
    "        return res\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def rearrangeArray(self, nums: List[int]) -> List[int]:\n",
    "        li1 = [i for i in nums if i > 0]\n",
    "        li2 = [i for i in nums if i < 0]\n",
    "        ans = []\n",
    "        for i in range(len(li1)):\n",
    "            ans.append(li1[i])\n",
    "            ans.append(li2[i])\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def rearrangeArray(self, nums: List[int]) -> List[int]:\n",
    "        res1 = []\n",
    "        res2 = []\n",
    "        res = []\n",
    "        for i in nums:\n",
    "            if i > 0:\n",
    "                res1.append(i)\n",
    "            if i < 0:\n",
    "                res2.append(i)\n",
    "        for i in range(len(res1)):\n",
    "            res.append(res1[i])\n",
    "            res.append(res2[i])\n",
    "        return res\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def rearrangeArray(self, nums: List[int]) -> List[int]:\n",
    "        ans = [0]*len(nums)\n",
    "        z = 0\n",
    "        f = 1\n",
    "        for num in nums:\n",
    "            if num > 0:\n",
    "                ans[z] = num\n",
    "                z+=2\n",
    "            else:\n",
    "                ans[f] = num\n",
    "                f += 2\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def rearrangeArray(self, nums: List[int]) -> List[int]:\n",
    "        neg, pos = [], []\n",
    "        for each in nums:\n",
    "            if each < 0:\n",
    "                neg.append(each)\n",
    "            else:\n",
    "                pos.append(each)\n",
    "\n",
    "        res = []\n",
    "        for i in range(len(nums) // 2):\n",
    "                res.append(pos[i])\n",
    "                res.append(neg[i])\n",
    "        return res\n"
   ]
  }
 ],
 "metadata": {},
 "nbformat": 4,
 "nbformat_minor": 2
}
